//1.引入express模块
require('dotenv').config()
const express = require('express')
// const bodyParser = require("body-parser")
const cors = require("cors")
const mongoose = require('mongoose')
const ROUTES = [
    require("./src/routes/hero"),
    require("./src/routes/user"),
    require("./src/routes/department"),
    require("./src/routes/role"),
    require("./src/routes/menu")
]
//2.创建app对象
const app = express();
app.use(express.json());
app.use(cors());
app.use(express.urlencoded({ extended: true }))
//数据库连接
mongoose.connect(process.env.MONGODB_URL, {
    serverSelectionTimeoutMS: 5000, // 设置服务器选择超时时间
    socketTimeoutMS: 45000 // 设置套接字超时时间
}).then(() =>
    console.log('数据库连接成功')
).catch(err => {
    console.error('数据库连接失败:', err.message);
    process.exit(1); // 连接失败时退出进程
});
// 统一路由注册
ROUTES.forEach(route => app.use('/api', route))

//定义服务启动端口
app.listen(process.env.PORT, () => {
    console.log('app listening on port 3000.')
})